<?php
namespace app\serversms\logic;

use \app\manage\logic\BaseLogic;

/**
 * @author:      李颖
 * @DateTime:    2017-06-26 11:00:28
 * @Description: 手机短信
 */
class Sms extends BaseLogic
{
	protected $name = 'sms';
    protected $config = 'db_config_sms';
    protected $smsModel = 'app\serversms\model\Sms';
    protected $cacheTime = '86400';

    use \app\serveruser\traits\addAppId;

    //内部db查询该条记录信息
    public function dbOneInfo($data)
    {
        $_where['id'] = $data['id'];
        $_where['status'] = isset($data['status']) ? $data['status'] : ['neq', -1];
        return $this->db()
                    ->where($_where)
                    ->find();
    }

    /*检验识别码的唯一性*/
    public function checkAction($data): array 
    {
        $info = $this->db()->where(['action'=>$data['action']])->find();
        if($info){
            return auto_failure(null, '该识别码action已存在');
        }else{
            return auto_success(null, '识别码有效');
        }
    }

    /*发送单条信息*/   
    public function sendOne($data): array 
    {
        $sms = new $data['smsName']();
        $res = $sms->sendOneSms($data);

        $logData['mobile']     = $data['send_info']['mobile'];
        $logData['text']       = $data['send_info']['text'];
        $logData['status']     = $res['code'];
        $logData['status_msg'] = $res['msg'];
        logic('app\serversms\logic\SmsLog')->addLog($logData); //添加短信发送记录

        if($res['code'] != 0){  
            return auto_failure($res, $res['detail']);
        }
        return auto_success($res, '成功发送短信');
    }

    /*发送多条短信*/
    public function batchSend($data): array 
    {
        $sms = new $data['smsName']();
        $res = $sms->sendBatchSms($data);

        $logData = [];

        if(isset($res['data'])){
            foreach ($res['data'] as $k => $v) {
                $logData[$k]['mobile']     = $v['mobile'];
                $logData[$k]['text']       = $data['send_info']['text'];
                $logData[$k]['status']     = $v['code'];
                $logData[$k]['status_msg'] = $v['msg'];
            }
            logic('app\serversms\logic\SmsLog')->batchAddLog($logData); //批量添加短信发送记录 
        }

        if(isset($res['total_count']) && $res['total_count'] > 0){
            return auto_success($res, '成功发送' . $res['total_count'] . '条短信');
        }else{
            return auto_failure($res, $res['detail']);
        }
        
    }

    /*添加单个短信配置*/
    public function addSms($data): array 
    {
        $res = $this->model($this->smsModel)->save($data);
        return auto_return($res);
    }

    /*获取某个应用下的所有短信配置*/
    public function smsList($data): array 
    {
        $res = cache('sms_smsList');   
        $res = 0;   
        if(!$res){
            $_where['status'] = 1;
            if(isset($data['app_id'])){   
                $manageAppId = \app\manage\service\ApiBase::getAppId();
                $_whereOr = $manageAppId == $data['app_id'] ? true : '`app_id`=' . $data['app_id'] . ' OR FIND_IN_SET(' . $data['app_id'] . ',`other_app_read`) OR is_public = 1';
            }else{
                $_whereOr = true;
            }
            $res = $this->model($this->smsModel)
                    ->where($_where)
                    ->where($_whereOr)
                    ->order('id', 'desc')
                    ->paginate(null,false,['page'=>$data['page'],'list_rows'=>$data['listRows']])
                    ->toArray();

            cache('sms_smsList', $res, $this->cacheTime);
        }
        return auto_return_page($res);
    }

    /*获取某个短信配置详情*/
    public function smsInfo($data): array 
    {
        isset($data['id']) && $_where['id'] = $data['id'];
        isset($data['action']) && $_where['action'] = $data['action'];
        $_where['status'] = 1;

        if(isset($data['app_id'])){   
            $manageAppId = \app\manage\service\ApiBase::getAppId();
            $_whereOr = $manageAppId == $data['app_id'] ? true : '`app_id`=' . $data['app_id'] . ' OR FIND_IN_SET(' . $data['app_id'] . ',`other_app_read`) OR is_public = 1';
        }else{
            $_whereOr = true;
        }

        $res = $this->model($this->smsModel)->where($_where)->where($_whereOr)->find();
        return auto_return($res, '成功获取短信配置', '获取短信配置失败');
    }

    /*修改某个短信配置*/
    public function editSms($data): array 
    {
        $_where['id'] = $data['id'];
        $_where['status'] = 1;

        $_where = $this->_addAppId($_where, $data['app_id']);
        unset($data['app_id']);
        $res = $this->model($this->smsModel)->save($data, $_where);
        cache('sms_smsList', NULL);   //清空缓存

        return auto_return($res);
    }

    /*删除某个短信配置*/
    public function delOneSms($data): array 
    {
        $_where['id'] = $data['id'];
        $_where['status'] = 1;
        $_where = $this->_addAppId($_where, $data['app_id']);
        $res = $this->db()->where($_where)->update(['status'=>'-1']);
        return auto_return($res);
    }
}